home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / DEVEL2.ZIP / PCXMODEY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-22  |  2.6 KB  |  136 lines

  1. /* Load a PCX file in mode Y */
  2.  
  3. /* Written by Bernie Roehl, April 1992 based on code by Dave Stampe */
  4.  
  5. #include <stdio.h>
  6. #include <dos.h>
  7. #include <alloc.h>
  8.  
  9. static int getbyte(int *c, int *count, FILE *in)
  10.     {
  11.     if (feof(in)) return EOF;
  12.     *c = getc(in) & 0xFF;
  13.     if ((*c & 0xC0) == 0xC0) {
  14.         *count = *c & 0x3F;
  15.         if (feof(in)) return EOF;
  16.         *c = getc(in) & 0xFF;
  17.         }
  18.     else
  19.         *count = 1;
  20.     return NULL;
  21.     }
  22.  
  23. static void putbyte(int c, int count, FILE *out)
  24.     {
  25.     if (count == 0)
  26.         return;
  27.     if (count > 1 || (c & 0xC0) == 0xC0)
  28.         putc(count | 0xC0, out);
  29.     putc(c, out);
  30.     }
  31.  
  32. static unsigned char far *screen = MK_FP(0xA000, 0);
  33.  
  34. static void write_line(char far *buffer, char far *scr)
  35. {
  36.     int i, plane;
  37.     char *add, *buff;
  38.  
  39.     for (plane = 0; plane < 4; ++plane)
  40.         {
  41.         outport(0x3C4, (1<<(plane+8))+2);
  42.         add = scr;
  43.         buff = &buffer[plane];
  44.         for (i = 0; i < 80; ++i)
  45.             {
  46.             *add++ = *buff;
  47.             buff += 4;
  48.             }
  49.         }
  50.     }
  51.  
  52. load_pcx(FILE *in, int page)
  53.     {
  54.     int c, count;
  55.     char buff[330];
  56.     char *buffer = &buff[0];
  57.     char *scr = &screen[16000*page];
  58.     unsigned nread = 0;
  59.  
  60.     reset_hdwe();
  61.     fseek(in, 128L, SEEK_SET);  /* skip PCX header */
  62.     while (getbyte(&c, &count, in) != EOF)
  63.     while (count--)
  64.         {
  65.         *buffer++ = c;
  66.         if (++nread == 320)
  67.             {
  68.             write_line(&buff[0],scr);
  69.             buffer = &buff[0];
  70.             nread = 0;
  71.             scr += 80;
  72.             }
  73.         }
  74.     return 0;
  75.     }
  76.  
  77. struct {
  78.     unsigned char manu, hard, encod, bitpx;
  79.     unsigned int x1, y1, x2, y2;
  80.     unsigned int hres, vres;
  81.     unsigned char palette[48];
  82.     unsigned char vmode, nplanes;
  83.     unsigned int bytesPerLine;
  84.     char unused[128-68];
  85.     } pccHeader = { 10, 5, 1, 8, 0, 0, 319, 199, 75, 75, { 0 }, 0x13, 1, 320, 0 };
  86.  
  87. struct { unsigned char r, g, b; } palbuff[256];
  88.  
  89. static char get_pixel(unsigned int adr, int page)
  90. {
  91.     outport(0x3CE, ((adr&3)<<8)+4);    /* select plane to read */
  92.     return *(char *)(MK_FP(0xA000+1000*page,adr>>2));
  93. }
  94.  
  95.  
  96. save_pcx(FILE *out, int page)
  97. {
  98.     unsigned c, oldc, count;
  99.     unsigned nput = 1;
  100.     union REGS r;
  101.  
  102.     reset_hdwe();
  103.     fwrite(&pccHeader, 128, 1, out);
  104.     count = 1;
  105.     oldc = get_pixel(0,page);
  106.     while ((nput>>2) < 16000)
  107.         {
  108.         c = get_pixel(nput++, page);
  109.         if (c != oldc)
  110.             {
  111.             putbyte(oldc, count, out);
  112.             oldc = c;
  113.             count = 1;
  114.             }
  115.         else if (++count >= 63)
  116.             {
  117.             putbyte(oldc, count, out);
  118.             count = 0;
  119.             }
  120.         }
  121.     putbyte(oldc, count, out);
  122.  
  123.     putc(0x0C, out);
  124.     for (count = 0; count < 256; ++count)
  125.         {
  126.         r.x.ax = 0x1015;
  127.         r.x.bx = count;                   /* write pallete */
  128.         int86(0x10, &r, &r);
  129.         putc(r.h.dh<<2, out);
  130.         putc(r.h.ch<<2, out);
  131.         putc(r.h.cl<<2, out);
  132.         }
  133.     return 0;
  134.     }
  135.  
  136.